package com.yutian.commandmysql;

import java.util.Scanner;

public class CommandMysqlApp {
    public static void main(String[] args) {

        if(null == args || args.length < 4){
            System.out.println("未设置启动参数!");
        } else {
            handleMysqlDb(args);
        }

        Scanner scan = new Scanner(System.in);
        System.out.println("输入exit结束程序，或继续输入启动参数（空格分隔，如‘-h127.0.0.1 -P3306 -uroot -p123456’）：");
        while (scan.hasNextLine()) {
            // This method may block while waiting for input
            // 判断是否还有输入
            String inputString = scan.nextLine();
            System.out.println("输入为： ["+ inputString + "]");
            if("exit".equals(inputString)){
                System.out.println("程序结束!");
                return;
            }
            String[] inputs = inputString.split(" ");
            if(null == inputs || inputs.length < 4){
                System.out.println("未设置正确的启动参数!");
                continue;
            }
            handleMysqlDb(inputs);
            System.out.println("输入exit结束程序，或继续输入启动参数（空格分隔，如‘-h127.0.0.1 -P3306 -uroot -p123456’）：");
        }
        System.out.println("程序结束!");
    }

    /**
     * -h127.0.0.1 -P3306 -uroot -p123456
     * @param args
     * @return
     */
    private static void handleMysqlDb(String[] args) {
        DbConfig db = new DbConfig();
        db.setIp(args[0].startsWith("-h") ? args[0].substring(2) : args[0]);
        db.setPort(args[1].startsWith("-P") ? args[1].substring(2) : args[1]);
        db.setUsername(args[2].startsWith("-u") ? args[2].substring(2) : args[2]);
        db.setPassword(args[3].startsWith("-p") ? args[3].substring(2) : args[3]);
        System.out.println("db参数如下：" + db.toString());

        // 预先创建数据库 create database testyt;
        createTable(db, "testyt");
        truncate(db, "testyt");
        insert(db, "testyt");
        findAll(db, "testyt");
        update(db, "testyt");
        findAll(db, "testyt");
    }

    private static void createTable(DbConfig dbConfig, String database){
        String sql = "CREATE TABLE student " +
                "(id INTEGER not NULL, " +
                " first VARCHAR(255), " +
                " last VARCHAR(255), " +
                " age INTEGER, " +
                " PRIMARY KEY ( id ))";
        Object[] params = new Object[0];
        JdbcUtils.execute(dbConfig, database, sql, params);
    }

    private static void truncate(DbConfig dbConfig, String database){
        String sql = "truncate table student";
        Object[] params = new Object[0];
        JdbcUtils.execute(dbConfig, database, sql, params);
    }

    private static void insert(DbConfig dbConfig, String database){
        String sql = "insert into student(id, first, last, age) values(?, ?, ?, ?)";
        Object[] params = new Object[]{1, "yutian", "nanjing", 31};
        JdbcUtils.executeUpdate(dbConfig, database, sql, params);
    }

    private static void update(DbConfig dbConfig, String database){
        String sql = "update student set age=? where id=?";
        Object[] params = new Object[]{21, 1};
        JdbcUtils.executeUpdate(dbConfig, database, sql, params);
    }

    private static void findAll(DbConfig dbConfig, String database){
        String sql = "select * from student";
        Object[] params = new Object[0];
        JdbcUtils.query(dbConfig, database, sql, params);
    }
}
